package com.thd.springboottest.camunda.listener;

import com.thd.springboottest.camunda.service.WorkFlowService;
import com.thd.springboottest.camunda.utils.SpringUtils;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.springframework.stereotype.Component;

/**
 * com.thd.springboottest.camunda.listener.ProcessInstanceListener
 *
 * @author: wanglei62
 * @DATE: 2022/6/1 8:59
 **/

public class CamundaListener implements TaskListener, ExecutionListener {
    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        String eventName = delegateExecution.getEventName();
        System.out.println("Execution Listener , event : " + eventName);

        WorkFlowService workFlowService = (WorkFlowService)SpringUtils.getBean("WorkFlowServiceImplForCamunda");
        System.out.println(workFlowService);

        if("create".equals(eventName)){    // 流程扭转

            System.out.println("create===========流程扭转");
        }else if("assigment".equals(eventName)){
            System.out.println("assigment===========流程部署");
        }else if("complete".equals(eventName)){
            System.out.println("complete===========流程完成");
        }else if("delete".equals(eventName)){
            System.out.println("delete===========流程结束");
        }else if("start".equals(eventName)){

            /**
             * 注意：
             * 在start的时候在数据库中还没有产生流程实例id!!!!!!
             * 所以调用API根据procInstId(delegateExecution.getProcessInstanceId())查询 流程实例是查不到的
             */
            System.out.println("start===========流程启动");
        }else if("end".equals(eventName)){
            System.out.println("end===========流程结束");
        }



        System.out.println("监听,流程实例ID：" + delegateExecution.getProcessInstanceId());

    }

    @Override
    public void notify(DelegateTask delegateTask) {

        /**
         * 注意：
         * 此时在数据库中还没有产生新的TASK!!!!!!
         * 所以调用API根据当前的taskId(delegateTask.getId())查询 Task是查不到的
         */
        WorkFlowService workFlowService = (WorkFlowService)SpringUtils.getBean("WorkFlowServiceImplForCamunda");
        System.out.println(workFlowService);

        String eventName = delegateTask.getEventName();

        String businessKey = delegateTask.getExecution().getProcessInstance().getBusinessKey();

        System.out.println("Task Listener , event : " + eventName);
        System.out.println("监听,待办ID：" + delegateTask.getId());
        System.out.println("---- BUSINESS KEY：" + businessKey);
        System.out.println("---- 待办KEY：" + delegateTask.getTaskDefinitionKey());
        System.out.println("---- 待办名称：" + delegateTask.getName());

    }
}
